package com.david.viewtest.view

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class MySysSwitch (
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {
    private val paint = Paint()

    private val padding = 11f

    private var halfHeight = 0f

    var dotLeft = 0f
    var lastDotLeft = 0f
    var dotAnimDistance = 0f

    var isOn = false
    /**
     * 动画对象
     */
    private val valueAnimator: ValueAnimator = ValueAnimator.ofFloat(0f, 1f)
    /**
     * 动画执行进度，0-1
     */
    private var animProgress = 0f


    /**
     * 动画执行相关的变量
     */
    private val checkedColor = Color.parseColor("#242b29")
    private val normalBgColor = Color.parseColor("#8E9496")
    private val rColorBgDis = 0x8E - 0x24
    private val gColorBgDis = 0x94 - 0x2b
    private val bColorBgDis = 0x96 - 0x29

    private val normalDotColor = Color.GRAY//0xFF888888
    private val rColorDotDis = 0x88 - 0x00
    private val gColorDotDis = 0x88 - 0xff //00FF00
    private val bColorDotDis = 0x88 - 0x00

    private var bgColor = normalBgColor
    private var dotColor = normalDotColor

    init {
        setOnClickListener {
            lastDotLeft = dotLeft

            if (isOn) {
                dotAnimDistance = height.toFloat() - width
            } else {
                dotAnimDistance = width - height.toFloat()
            }
            isOn = !isOn
            valueAnimator.start()
        }

        valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator) {
                animProgress = animation.animatedValue as Float
                dotLeft = lastDotLeft + dotAnimDistance * animProgress


                 if (isOn) {
                    val tr = (0x24 + (1 - animProgress) * rColorBgDis).toInt()
                    val tg = (0x2b + (1 - animProgress) * gColorBgDis).toInt()
                    val tb = (0x29 + (1 - animProgress) * bColorBgDis).toInt()
                     bgColor =((0xFF shl 24) or (tr shl 16) or (tg shl 8) or tb)


                     val dr = (0x00 + (1 - animProgress) * rColorDotDis).toInt()
                     val dg = (0xFF + (1 - animProgress) * gColorDotDis).toInt()
                     val db = (0x00 + (1 - animProgress) * bColorDotDis).toInt()
                     dotColor =((0xFF shl 24) or (dr shl 16) or (dg shl 8) or db)

                 } else {
                    val tr = (0x24 + animProgress * rColorBgDis).toInt()
                    val tg = (0x2b + animProgress * gColorBgDis).toInt()
                    val tb = (0x29 + animProgress * bColorBgDis).toInt()
                     bgColor =((0xFF shl 24) or (tr shl 16) or (tg shl 8) or tb)

                     val dr = (0x00 + animProgress * rColorDotDis).toInt()
                     val dg = (0xFF + animProgress * gColorDotDis).toInt()
                     val db = (0x00 + animProgress * bColorDotDis).toInt()
                     dotColor =((0xFF shl 24) or (dr shl 16) or (dg shl 8) or db)
                }


                postInvalidate()
            }

        })

        valueAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}

            override fun onAnimationEnd(animation: Animator) {
            }

            override fun onAnimationCancel(animation: Animator) {}

            override fun onAnimationRepeat(animation: Animator) {}
        })
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        halfHeight = measuredHeight / 2f
    }

    override fun onDraw(canvas: Canvas) {
        //super.onDraw(canvas)
        paint.color = bgColor
        canvas.drawRoundRect(height/3f, height/3f, width.toFloat() - height/3f, height.toFloat() - height/3f, halfHeight, halfHeight, paint)
        paint.color = dotColor
        canvas.drawCircle(dotLeft + halfHeight, halfHeight, halfHeight - padding, paint)
    }
}